package com.training.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.base.BaseActivity;
import com.training.bean.User;
import com.training.databinding.ActivityLoginBinding;
import com.training.event.DefaultEvent;
import com.training.net.Constant;
import com.training.net.HttpResponse;

import org.greenrobot.eventbus.EventBus;

import com.training.net.NetCallback;
import com.training.util.ToastyUtil;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LoginActivity extends BaseActivity<ActivityLoginBinding> {
    @Override
    public ActivityLoginBinding binding() {
        return ActivityLoginBinding.inflate(getLayoutInflater());
    }

    @Override
    public void onCreated(Bundle savedInstanceState) {
        mBinding.loginBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = mBinding.usernameInput.getEditText().getText().toString().trim();
                String password = mBinding.passwordInput.getEditText().getText().toString().trim();
                dc.login(username, password, new NetCallback<HttpResponse<User>>() {

                    @Override
                    public void onError(int code, String message) {
                        ToastyUtil.showError(context, message, Toast.LENGTH_SHORT, true);
                    }

                    @Override
                    public void onSuccess(HttpResponse<User> response) {
                        dc.setUser(response.data);
                        startActivity(new Intent(context, HomeActivity.class));
                        EventBus.getDefault().post(new DefaultEvent(1001, "登录成功！"));
                        // 成功提示
                        ToastyUtil.showSuccess(context, "登录成功！", Toast.LENGTH_SHORT, true);
                    }

                    @Override
                    public void onFinally() {

                    }
                });
            }
        });
    }

    @Override
    public void loadData() {

    }

    @Override
    public void refreshUI() {

    }

    @Override
    public void onDefaultEvent(DefaultEvent mEvent) {
        super.onDefaultEvent(mEvent);
        if (mEvent.code == 1001) {
            startActivity(new Intent(context, HomeActivity.class));
        }
    }
}
